<?php
// @formatter:off
/**
 * @file WCTheme.php
 * @author Alejandro Dario Simi
 * @date $Date: 2013-10-27 18:31:53 +0000 (Sun, 27 Oct 2013) $
 *
 * $Id: WCTheme.php 110 2013-10-27 18:31:53Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/trunk/includes/themes/WCTheme.php $
 */
// @formatter:on

require_once "{$wcPaths["includes-directory"]}/themes/WCThemeMenu.php";
require_once "{$wcPaths["includes-directory"]}/themes/WCThemeMenusHolder.php";

/**
 * @abstract
 * @class WCTheme This class holds the basic logic required from a wcomix them.
 */
abstract class WCTheme {
	/**
	 * @var string Theme's name.
	 */
	protected $_name;
	/**
	 * @var WCParser Pointer to a parser in charge of analyzing this theme's
	 * templates.
	 */
	protected $_parser = false;
	/**
	 * @var string Theme's full system path.
	 */
	protected $_themeRoot = false;
	/**
	 * @var string Theme's full URI.
	 */
	protected $_themeUri = false;
	//
	// Construction.
	/**
	 * Class constructor. It calls theme's main loader and tools and adds
	 * loaders.
	 */
	public function __construct() {
		//
		// Calling main loader.
		$this->load();
		//
		// Calling Right-Tools loader.
		$this->loadRightTools();
		//
		// Calling Right-Adds loader.
		$this->loadRightAdds();
	}
	//
	// Public methods.
	/**
	 * This method allows to get this theme's name.
	 *
	 * @return string Theme's name.
	 */
	public function name() {
		return $this->_name;
	}
	/**
	 * This method allows to get the parser in charge of analyzing this
	 * theme's templates.
	 *
	 * @return WCParser Returns a parser's pointer.
	 */
	public function & parser() {
		return $this->_parser;
	}
	/**
	 * This method allows to get the theme's full system path.
	 *
	 * @return string Returns a full system path.
	 */
	public function root() {
		return $this->_themeRoot;
	}
	/**
	 * This method allows to get the theme's full URI.
	 *
	 * @return string Returns an URI.
	 */
	public function uri() {
		return $this->_themeUri;
	}
	//
	// Protected methods.
	/**
	 * This method should load all the stuff a theme requires to work
	 * properly.
	 * Also, It triggers a page build up from templates based on $wcAction.
	 */
	protected function load() {
		die(__CLASS__."::".__FUNCTION__."() Subclass Resposabillity\n");
	}
	/**
	 * This method load all right advertisements managers and also set their
	 * templates to be shown
	 */
	protected function loadRightAdds() {
		//
		// Loading global pointers.
		// @{
		global $wcRightAdds;
		global $wcThemeAssigns;
		global $wcDefaults;
		// @}
		//
		// Creating an auxiliary list for accepted templates.
		$list = array();
		//
		// Going through all right tools and analyzing them.
		foreach($wcRightAdds as $add) {
			//
			// Checking if the right tool requires some PHP analysis.
			if(isset($add["manager"])) {
				//
				// Checking if the PHP file required exists. If it
				// does, it is included, otherwise an exception is
				// raised.
				if(is_readable($add["manager"])) {
					require_once $add["manager"];
				} else {
					trigger_error(__CLASS__."::".__FUNCTION__."() Error: Unable to load '{$add["manager"]}'", E_USER_ERRORu);
				}
			}
			//
			// If no valid template was specified, an exception is
			// raised.
			if(!is_readable($add["template"])) {
				trigger_error(__CLASS__."::".__FUNCTION__."() Error: Unable to load '{$add["template"]}'", E_USER_ERRORu);
			}
			//
			// Checking how many times this ad should be included.
			$times = isset($add["times"]) ? $add["times"] : 1;
			//
			// Including this ad as many times as needed.
			$time = 0;
			while($time < $times) {
				//
				// Setting a global indicator of iteration.
				$wcDefaults["WCTheme::time"] = $time;
				//
				// Auxiliary flag.
				$ok = true;
				//
				// If there is a function specified, it is
				// executed and if it returns true, the tool is
				// added.
				if(isset($add["checker"])) {
					$ok = $add["checker"]();
				}
				//
				// If everything is ok, the tool is added to the
				// list.
				if($ok) {
					$list[] = array("template" => $add["template"]);
				}
				//
				// Increasing iteration counter.
				$time++;
			}
		}
		//
		// Setting the list to be shown.
		if(count($list)) {
			$wcThemeAssigns["WC-RIGHT-ADDS"] = $list;
		}
	}
	/**
	 * This method load all right tools managers and also set their templates
	 * to be shown
	 */
	protected function loadRightTools() {
		//
		// Loading global pointers.
		// @{
		global $wcRightTools;
		global $wcThemeAssigns;
		global $wcDefaults;
		// @}
		//
		// Creating an auxiliary list for accepted templates.
		$list = array();
		//
		// Going through all right tools and analyzing them.
		foreach($wcRightTools as $tool) {
			//
			// Checking if the right tool requires some PHP analysis.
			if(isset($tool["manager"])) {
				//
				// Checking if the PHP file required exists. If it
				// does, it is included, otherwise an exception is
				// raised.
				if(is_readable($tool["manager"])) {
					require_once $tool["manager"];
				} else {
					trigger_error(__CLASS__."::".__FUNCTION__."() Error: Unable to load '{$tool["manager"]}'", E_USER_ERRORu);
				}
			}
			//
			// If no valid template was specified, an exception is
			// raised.
			if(!is_readable($tool["template"])) {
				trigger_error(__CLASS__."::".__FUNCTION__."() Error: Unable to load '{$tool["template"]}'", E_USER_ERRORu);
			}
			//
			// Checking how many times this tool should be included.
			$times = isset($tool["times"]) ? $tool["times"] : 1;
			//
			// Including this tool as many times as needed.
			$time = 0;
			while($time < $times) {
				//
				// Setting a global indicator of iteration.
				$wcDefaults["WCTheme::time"] = $time;
				//
				// Auxiliary flag.
				$ok = true;
				//
				// If there is a function specified, it is
				// executed and if it returns true, the tool is
				// added.
				if(isset($tool["checker"])) {
					$ok = $tool["checker"]();
				}
				//
				// If everything is ok, the tool is added to the
				// list.
				if($ok) {
					$aux = array("template" => $tool["template"]);
					if(isset($tool["title"])) {
						$aux["title"] = $tool["title"];
					}

					$list[] = $aux;
				}
				//
				// Increasing iteration counter.
				$time++;
			}
		}
		//
		// Setting the list to be shown.
		$wcThemeAssigns["WC-RIGHT-TOOLS"] = $list;
	}
	//
	// Public class methods.
	/**
	 * This class method is a subclass responsibility and allows a theme to
	 * install all the stuff it requires.
	 *
	 * @return boolean Returns true when the installation was a success.
	 */
	public static function Install() {
		return true;
	}
}
?>
